package com.inno.life.infra.rpc;

import com.inno.auth.api.UserFeignService;
import com.inno.auth.entity.AuthUserDTO;
import com.inno.auth.entity.CommonReturnType;
import com.inno.life.infra.entity.UserInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * @author sean
 */
@Component
public class UserRpc {
    @Resource
    private UserFeignService userFeignService;

    /**
     * 根据id获取用户信息
     */
    public UserInfo getUserInfoById(Long id) {
        AuthUserDTO authUserDTO = new AuthUserDTO();
        authUserDTO.setId(id);
        CommonReturnType<AuthUserDTO> returnType = this.userFeignService.getUserInfo(authUserDTO);
        UserInfo userInfo = new UserInfo();
        if (returnType.getCode() != 200){
            throw new RuntimeException(returnType.getMessage());
        }
        AuthUserDTO data = returnType.getContent();
        userInfo.setId(data.getId());
        userInfo.setUsername(data.getUsername());
        userInfo.setAvatar(data.getAvatar());
        return userInfo;
    }

    public List<UserInfo> getUserInfoByIdList(List<Long> userIdList) {
        CommonReturnType<List<AuthUserDTO>> returnType = this.userFeignService.getUserInfoByIdList(userIdList);
        List<UserInfo> userInfoList = new LinkedList<>();
        if (returnType.getCode() != 200){
            throw new RuntimeException(returnType.getMessage());
        }
        List<AuthUserDTO> authUserDTOList = returnType.getContent();

        for (AuthUserDTO authUserDTO : authUserDTOList) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(authUserDTO.getId());
            userInfo.setUsername(authUserDTO.getUsername());
            userInfo.setAvatar(authUserDTO.getAvatar());
            userInfoList.add(userInfo);
        }
        return userInfoList;
    }

    public Boolean checkLogin(AuthUserDTO authUserDTO) {
        CommonReturnType<Boolean> returnType = this.userFeignService.checkLogin(authUserDTO);
        return returnType.getContent();
    }
}
